<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-s-custom"></i> 班级管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>
                <el-input v-model="query.cnameK" placeholder="班级名称" class="handle-input mr10" clearable></el-input>
                <el-input v-model="query.teacher_nameK" placeholder="辅导员姓名" class="handle-input mr10" clearable></el-input>
                <el-select v-model="query.anoK" filterable placeholder="所属学院" class="handle-input mr10" clearable>
                    <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="danger" icon="el-icon-plus" @click="handleAdd">新增</el-button>




            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="cname" label="班级名称"></el-table-column>
                <el-table-column prop="teacherName" label="辅导员姓名"></el-table-column>
                <el-table-column prop="teacherTel" label="辅导员电话"></el-table-column>
                <el-table-column prop="aname" label="所属学院"></el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-tickets"
                            @click="queryStudents(scope.$index, scope.row)"
                        >查询班级学生</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handlePageChange"
                    :current-page="query.pageIndex"
                    :page-sizes="[10, 50, 100, 500]"
                    :page-size="query.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageTotal">
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :title="title" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="班级名称">
                    <el-input v-model="form.cname"></el-input>
                </el-form-item>
                <el-form-item label="导员姓名">
                    <el-input v-model="form.teacherName"></el-input>
                </el-form-item>
                <el-form-item label="导员电话">
                    <el-input v-model="form.teacherTel"></el-input>
                </el-form-item>
                <el-form-item label="所属学院">
                    <el-select v-model="form.ano" filterable placeholder="所属学院">
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="done">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>

export default {
    name: 'testTable',
    data() {
        return {
            title: "",
            query: {
                // 提供查询项：辅导员姓名，电话和所属学院
                teacher_nameK: '',
                cnameK: '',
                anoK: null,//所属学院为精准查询
                pageIndex: 1,
                pageSize: 10
            },
            // 表格数据
            tableData: [],
            multipleSelection: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            // 所属学院下拉选项
            options: [],
        };
    },



    created() {
        this.getData();
    },
    beforeRouteEnter(to, from, next) {
        next(vm => {
            vm.jumpIn();
        });
    },
    methods: {
        getData() {
            // 获取表格数据
            this.$http.post('/class/selectAll',{
                currentPage: this.query.pageIndex,
                pageSize: this.query.pageSize,
                anoK: this.query.anoK,
                teacherNameK: this.query.teacher_nameK,
                cnameK: this.query.cnameK
                }).then((res)=>{//回调函数
                this.tableData = res.data.records
                this.pageTotal = res.data.totalElements;

                //获取学院名
                for (let i = 0; i < this.tableData.length; i++) {
                    this.$http.get('/academy/' + this.tableData[i].ano).then((res)=>{//回调函数
                        this.$set(this.tableData[i], 'aname', res.data.data.aname);
                    })
                }
            })

            // 获取学院下拉菜单选项
            this.$http.get('/academy').then((res)=>{//回调函数
                this.options = res.data.map((item) => {
                    return {
                        value: item.ano,
                        label: item.aname
                    };
                });
        })
        },

        jumpIn(){
            if (!this.$route.query==null){
                this.query.teacher_nameK = ""
                this.query.cnameK = ""
                this.query.anoK = this.$route.query.row.ano;
            }
            this.getData();
        },

        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 新增操作
        handleAdd(){
            console.log(this.$router.aca)
            this.form = {};
            this.title = "新增班级";
            this.editVisible = true;
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$http.delete('/class/' + row.cno).then((res)=>{//回调函数
                        if (res.data.code === 200) {
                            this.getData();
                            this.$message.success('删除成功');
                        }else {
                            console.log(res)
                            this.$message.error(res.data.message);
                        }
                    })
                })
        },
        //todo
        //查询班级学生操作
        queryStudents(index, row) {
                this.$router.push({
                    path: "/studentManage",
                    query: {
                        aca: row,
                        class: row
                    }
                });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            let cnos = this.multipleSelection.map(item => item.cno)
            this.$http.delete('/class/batchDelete', {data: cnos}).then((res)=>{//回调函数

                this.$message.error(`删除了` + res.data.data + '条数据');
                this.getData();

            })

            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.title = "编辑"
            //生成row的副本，防止修改原数据
            this.form = Object.assign({}, row);
            this.editVisible = true;
        },
        //点击确定按钮判断是保存还是更新
        done(){
            if(this.form.cno){//有id就是更新
                this.edit()
            }else {
                this.save()
            }
        },
        // 新增
        save() {
            console.log(1)
            this.$http.post("/class", this.form)
                .then((res)=>{
                    if (res.data.code === 200) {
                        this.editVisible = false;

                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                        this.getData();
                    }else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                })
        },
        // 编辑
        edit() {
            this.$http.put("/class", this.form)
                .then((res)=>{
                    if (res.data.code === 200) {
                        this.editVisible = false;

                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                        this.getData();
                    }else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                })
        },
        // 每页条数更改
        handleSizeChange(val) {
            this.$set(this.query, 'pageIndex', 1);
            this.$set(this.query, 'pageSize', val);
            this.getData();
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-input {
    width: 180px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}

</style>


